﻿using ML.Entity;
using ML.IBusiness;
using System.Web.Mvc;

namespace ML.Web.Controllers
{
    /// <summary>
    /// 产品控制器
    /// </summary>
    [Login]
    public class ProductController : BaseController
    {
        private readonly IProductBusiness productBusiness;
        private readonly IMerchantBusiness merchantBusiness;
        public ProductController(IProductBusiness _productBusiness, IMerchantBusiness _merchantBusiness)
        {
            productBusiness = _productBusiness;
            merchantBusiness = _merchantBusiness;
        }

        /// <summary>
        /// 产品管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 商户管理产品页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UserIndex()
        {
            string merchantId = UserHelper.GetCurrentUser().MerchantId;
            if (string.IsNullOrEmpty(merchantId))
            {
                var data = merchantBusiness.GetNoDelInfoByUserId(UserHelper.GetCurrentUserId());
                if (data.status_code == 200)
                {
                    //如果查询到内容，将其保存到Session中
                    UserInfoSession userInfoSession = UserHelper.GetCurrentUser();
                    userInfoSession.MerchantId = data.data.Id;
                    userInfoSession.MerchantName = data.data.Name;
                    UserHelper.SetSession(userInfoSession);

                    merchantId = data.data.Id;
                }
                else
                {
                    merchantId = "";
                }
            }

            ViewBag.MerchantId = merchantId;
            ViewBag.MerchantName = UserHelper.GetCurrentUser().MerchantName;
            return View();
        }

        /// <summary>
        /// 根据查询条件查询产品信息
        /// </summary>
        /// <param name="productSearch">产品查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult List(ProductSearch productSearch)
        {
            var data = productBusiness.List(productSearch);
            return JsonDate(data.data);
        }

        /// <summary>
        /// 根据查询条件查询产品信息
        /// </summary>
        /// <param name="productSearch">产品查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult ListUser(ProductSearch productSearch)
        {
            var merchantId = UserHelper.GetCurrentUser().MerchantId;
            if (string.IsNullOrEmpty(merchantId))
            {
                merchantId = "-1";
            }
            productSearch.merchantid_search = merchantId;
            var data = productBusiness.List(productSearch);
            return JsonDate(data.data);
        }

        /// <summary>
        /// 根据产品Id查询产品信息
        /// </summary>
        /// <param name="id">产品Id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetDetailById(string id)
        {
            var data = productBusiness.GetDetailById(id);
            return JsonDateTime(data);
        }

        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="entity">产品实体</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Add(ProductEntity entity)
        {
            entity.MerchantId = UserHelper.GetCurrentUser().MerchantId;
            var data = productBusiness.Add(entity);
            return Json(data);
        }

        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="entity">产品实体</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Update(ProductEntity entity)
        {
            var data = productBusiness.Update(entity);
            return Json(data);
        }

        /// <summary>
        /// 上下架产品
        /// </summary>
        /// <param name="id">产品Id</param>
        /// <param name="ualmark">上下架标示(0-未上架，1-已上架)</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult UalMark(string id, int ualmark)
        {
            var data = productBusiness.UalMark(id, ualmark);
            return Json(data);
        }

        /// <summary>
        /// 删除产品信息
        /// </summary>
        /// <param name="id">产品Id</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Delete(string id)
        {
            var data = productBusiness.Delete(id);
            return Json(data);
        }
    }
}